package com.example.computer_exam_training_sys.controller;


import com.example.computer_exam_training_sys.entity.UserWrongQuestionsInfo;
import com.example.computer_exam_training_sys.service.UserWrongQuestionsService;
import com.example.computer_exam_training_sys.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.example.computer_exam_training_sys.vo.QuestionWithPoint;
import com.example.computer_exam_training_sys.vo.UserWrongQuestionWithCourseVO;

@RestController
@RequestMapping("/wrongquestions")
@CrossOrigin
public class UserWrongQuestionsController {



    @Autowired
    private UserWrongQuestionsService service;

    // 查所有
    //http://localhost:8080/wrongquestions/all
    @GetMapping("/all")
    public List<UserWrongQuestionsInfo> findAll() {
        return service.findAll();
    }

    // 根据用户 ID 查找
    //http://localhost:8080/wrongquestions/byuser?userId=1
    @GetMapping("/byuser")
    public List<UserWrongQuestionsInfo> findByUserId(@RequestParam Integer userId) {
        return service.findByUserId(userId);
    }

    // 根据题目 ID 查找
    //http://localhost:8080/wrongquestions/byquestion?questionId=2
    @GetMapping("/byquestion")
    public List<UserWrongQuestionsInfo> findByQuestionId(@RequestParam Integer questionId) {
        return service.findByQuestionId(questionId);
    }

    // 插入（之前存在的插入数据会使之前存在的数据wrongCount加1）
    //http://localhost:8080/wrongquestions/insert
    //测试json{
    //  "userId": 2,
    //  "questionId": 2,
    //  "sourceType": 1,
    //  "sourceId": 3001,
    //  "wrongCount": 1,
    //  "lastWrongTime": "2025-05-29T10:30:00",
    //  "isMastered": 0,
    //  "isDeleted": 0
    //}
    @PostMapping("/insert")
    public int insert(@RequestBody UserWrongQuestionsInfo info) {
        return service.insert(info);
    }

    // 更新(更新错误次数或掌握状态)
    //http://localhost:8080/wrongquestions/update
    //测试json{
    //  "id": 1,
    //  "wrongCount": 3,
    //  "lastWrongTime": "2025-05-29T15:00:00",
    //  "isMastered": 1
    //}
    @PostMapping("/update")
    public int update(@RequestBody UserWrongQuestionsInfo info) {
        return service.update(info);
    }

    // 逻辑删除isDeleted=1
    //http://localhost:8080/wrongquestions/softdelete?id=1
    @GetMapping("/softdelete")
    public int softDelete(@RequestParam Integer id) {
        return service.softDelete(id);
    }

    // 物理删除
    //http://localhost:8080/wrongquestions/harddelete?id=1
    @GetMapping("/harddelete")
    public int hardDelete(@RequestParam Integer id) {
        return service.hardDelete(id);
    }

    // 新增接口：根据point_id查题目及知识点信息
// 访问路径: http://localhost:8080/question/withpoint?pointId=1
    @GetMapping("/withpoint")
    public List<QuestionWithPoint> getQuestionsWithPoint(@RequestParam Integer pointId) {
        return service.selectQuestionsWithPointByPointId(pointId);
    }

    // 新增接口：通过课程ID查找错题（含知识点和题目内容）
    // http://localhost:8080/wrongquestions/bycourse?courseId=1
    @GetMapping("/bycourse")
    public List<UserWrongQuestionWithCourseVO> findWrongQuestionsByCourseId(@RequestParam Integer courseId) {
        return service.findWrongQuestionsByCourseId(courseId);
    }

    @GetMapping("/query")
    public PageResult<UserWrongQuestionWithCourseVO> queryWrongQuestions(
            @RequestParam(required = false) Integer userId, // 新增参数
            @RequestParam(required = false) Integer courseId,
            @RequestParam(required = false) Integer pointId,
            @RequestParam(required = false) Integer isMastered,
            @RequestParam(required = false) Integer sourceType,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size
    ) {
        return service.queryWrongQuestions(
                userId, courseId, pointId, isMastered, sourceType, startTime, endTime, page, size
        );
    }
    
    // 查询用户累计错题数
    @GetMapping("/count/byuser")
    public int countByUserId(@RequestParam Integer userId) {
        return service.countDistinctQuestionsByUserId(userId);
    }
    // 批量查询错题详情（根据id列表）
    // GET /wrongquestions/byids?ids=1,2,3
    @GetMapping("/byids")
    public List<UserWrongQuestionWithCourseVO> findWrongQuestionsByIds(@RequestParam String ids) {
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        return service.findWrongQuestionsByIds(idList);
    }

    // 只更新掌握状态
// http://localhost:8080/wrongquestions/updatemastered
// 测试json: {"id": 1, "isMastered": 1}
    @PostMapping("/updatemastered")
    public int updateMastered(@RequestBody Map<String, Object> params) {
        Integer id = (Integer) params.get("id");
        Byte isMastered = ((Number) params.get("isMastered")).byteValue();
        return service.updateMastered(id, isMastered);
    }
}
